<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */

/**
 * 日期处理类
 */

class cls_date{
	
	public function cls_date()
	{
		;
	}
	
	//public function __construct()
	//{
	//	;
	//}
		
	/*
	 * 把时间转换为时间数组
	 * 时间格式形如"2010-04-22 13:25:45"
	 * 返回：
	 * Array
		(
    		[year] => 2010
    		[month] => 4
    		[day] => 22
    		[hour] => 13
    		[minute] => 25
    		[second] => 45
		)
	 */
	public function time_to_array($time)
	{
		$time=preg_replace('/\s+/',' ',$time);
		$time_array=explode(' ',$time);
		
		$date=explode('-',$time_array[0]);
		$time=explode(':',$time_array[1]);
		
		$result['year']=intval($date[0]);
		$result['month']=intval($date[1]);
		$result['day']=intval($date[2]);
		
		$result['hour']=intval($time[0]);
		$result['minute']=intval($time[1]);
		$result['second']=intval($time[2]);	

		return $result;
	}	
	
	/*
	 * 把日期转换为时间数组
	 * 日期格式形如"2010-04-22"
	 * 返回：
	 * Array
		(
    		[year] => 2010
    		[month] => 4
    		[day] => 22
    		[hour] => 0
    		[minute] => 0
    		[second] => 0
		)
	 */
	public function date_to_array($date)
	{
		$date=explode('-',$date);
	
		$result['year']=intval($date[0]);
		$result['month']=intval($date[1]);
		$result['day']=intval($date[2]);
		
		$result['hour']=0;
		$result['minute']=0;
		$result['second']=0;	

		return $result;
	}		
	
	/*
	 * 把时间转换为时间戳
	 * 时间格式形如"2010-04-22 13:25:45"
	 * 返回时间戳
	 */
	public function time_to_stamp($time)
	{
		$time_array=$this->time_to_array($time);
		return mktime($time_array['hour'],$time_array['minute'],$time_array['second'],$time_array['month'],$time_array['day'],$time_array['year']);
	}
	
	/*
	 * 把日期转换为时间戳
	 * 时间格式形如"2010-04-22"
	 * 返回时间戳
	 */
	public function date_to_stamp($date)
	{
		$date_array=$this->date_to_array($date);
		return mktime($date_array['hour'],$date_array['minute'],$date_array['second'],$date_array['month'],$date_array['day'],$date_array['year']);
	}	
	
	public function get_time($timestamp='')
	{
		if(empty($timestamp))
		{
			return date('Y-m-d : H:i:s');
		}
		else{
			return date('Y-m-d : H:i:s',$timestamp);
		}
	}
	
	public function get_date($timestamp='')
	{
		if(empty($timestamp))
		{
			return date('Y-m-d');
		}
		else{
			return date('Y-m-d',$timestamp);
		}
	}	
	
	public function format_date($year,$month,$day)
	{
		return $year.'-'.$month.'-'.$day;
	}
	
	/*
	 * 获得某一个月的第一天的日期
	 * 参数$date格式形如"2010-04-22"
	 * 返回"2010-04-01"
	 */
	public function get_first_day($date)
	{
		$date_array=$this->date_to_array($date);
		
		$year=$date_array['year'];
		$month=$date_array['month'];
		$day='01';
		
		$timestamp=$this->date_to_stamp($this->format_date($year,$month,$day));
		
		return $this->get_date($timestamp);
	}
	
	/*
	 * 获得某一个月的最后一天的日期
	 * 参数$date格式形如"2010-04-22"
	 * 根据大小月返回"2010-04-30"或"2010-04-31"
	 */
	public function get_last_day($date)
	{
		$date_array=$this->date_to_array($date);
		
		$year=$date_array['year'];
		$month=$date_array['month'];
		$day='01';
		
		if($month==12)
		{
			$month=1;
			$year=$year+1;
		}
		else{
			$month=$month+1;
		}
		
		$timestamp=$this->date_to_stamp($this->format_date($year,$month,$day));
		
		$timestamp=$timestamp-1;
		
		return $this->get_date($timestamp);
	}	
	
	/*
	 * 获取开始日期到结束日期之间的所有日期
	 * 如$star_date='2010-04-02',$end_date='2010-04-05'
	 * Array
		(
    		[0] => '2010-04-02'
  			[2] => '2010-04-03'
  			[3] => '2010-04-04'
  			[4] => '2010-04-05'
		)
	 */
	public function get_dates($star_date,$end_date)
	{
		$start_timestamp=!empty($star_date)? $this->date_to_stamp($star_date):time();	
		$end_timestamp=!empty($end_date)? $this->date_to_stamp($end_date):time();
		
		if($end_timestamp<$start_timestamp)//交换开始和结束日期
		{
			$tmp=$end_timestamp;
			$end_timestamp=$start_timestamp;
			$start_timestamp=$tmp;	
		}
		
		for($stamp=$start_timestamp;$stamp<=$end_timestamp;$stamp=$stamp+24*60*60)
		{
			$result[]=$this->get_date($stamp);
		}
		return $result;
	}
	
	
	/*
	 * 返回开始日期和结束日期之间的天数
	 */
	public function get_day_number($star_date,$end_date)
	{
		return count($this->get_dates($star_date,$end_date));
	}
	
	/*
	 * 获取开始日期到结束日期之间的所有日期对应的开始和结束时间戳
	 * 如$star_date='2010-01-01',$end_date='2010-01-03'
		Array
		(
    		[2010-01-01] => Array
        	(
            	[start_stamp] => 1262275200
            	[end_stamp] => 1262361599
        	)

    		[2010-01-02] => Array
        	(
            	[start_stamp] => 1262361600
            	[end_stamp] => 1262447999
        	)

    		[2010-01-03] => Array
        	(
            	[start_stamp] => 1262448000
            	[end_stamp] => 1262534399
        	)
		)
	 */	
	public function get_timestamps($star_date,$end_date)
	{
		$dates=$this->get_dates($star_date,$end_date);
		$result=array();
		
		if(!empty($dates))
		{
			foreach($dates as $key=>$date)
			{
				$stamps=array();
				$stamps['start_stamp']=$this->date_to_stamp($date);
				$stamps['end_stamp']=$this->date_to_stamp($date)+24*60*60-1;
				
				$result[$date]=$stamps;
			}
		}
		
		return $result;
	}
	
	public function get_microtime($microtime='')
	{
		if(empty($microtime))
		{
			$microtime=microtime();
		}
		$time=explode(' ',$microtime);
		$sec=$time[1];
		$micro_sec=$time[0];		
		
		$micro_sec=substr($micro_sec,1,strlen($micro_sec));
		
		return $sec.$micro_sec;
	}
	
	public function get_microtime_diff($microtime1,$microtime2)
	{
		if(empty($microtime1) || empty($microtime2))
		{
			return false;
		}
		else{
			$t1=$this->get_microtime($microtime1);
			$t2=$this->get_microtime($microtime2);
			
			if($t2>=$t1)
			{
				return floatval($t2-$t1);
			}
			else{
				return floatval($t1-$t2);
			}
		}
	}
}

?>